package cn.ict.cmsr.dpay.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.regex.Pattern;

import cn.ict.cmsr.dpay.model.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;
import org.panda.exception.code.APIServiceCode;
import org.panda.exception.code.CommonServiceCode;
import org.panda.exception.code.OrderServiceCode;
import org.springframework.stereotype.Service;

import com.cmdt.oscaraep.common.util.StringUtil;
import com.cmdt.oscaraep.common.util.TimeUtil;

import cn.ict.cmsr.dpay.dict.Const;
import cn.ict.cmsr.dpay.exception.OrderServiceException;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


/**
 * 	字段校验工具类
 *
 * @author xukw
 *
 */

@Slf4j
@Service
public class Validators {
	public static void valid(OrderCreateDto orderCreateDto) {
		if(orderCreateDto.getOrderPrice() == null) {
			log.error("string is empty");
			throw new OrderServiceException(OrderServiceCode.ORDER_PRICE_NOT_FOUND);
		}

		String orderTime = orderCreateDto.getOrderTime();
		Long longTime = TimeUtil.getLongTime(orderTime);
		LocalDateTime orderDateTime = Instant.ofEpochMilli(longTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
		LocalDateTime now = Instant.ofEpochMilli(System.currentTimeMillis()+60000).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
		log.info("==========longTime="+orderDateTime+",now="+now);
		if (orderDateTime.isAfter(now)) {
			log.error("下单时间不能为未来时间");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "下单时间不能为未来时间");
		}

		String expireTime = orderCreateDto.getExpireTime();
		long expire = TimeUtil.getLongTime(expireTime);
		if (longTime>expire) {
			log.error("超时时间要大于订单时间");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "超时时间要大于订单时间");
		}

	    int transportPrice = orderCreateDto.getOrderPrice().getTransportPrice();//运费金额
	    int couponReducedPrice = orderCreateDto.getOrderPrice().getCouponReducedPrice();//优惠券抵扣金额
	    int promotionReducedPrice = orderCreateDto.getOrderPrice().getPromotionReducedPrice();//促销优惠金额
	    int otherReducedPrice = orderCreateDto.getOrderPrice().getOtherReducedPrice();//其他优惠金额
	    int integralPrice = orderCreateDto.getOrderPrice().getIntegralPrice();//积分折现金额
	    int businessPaidPrice = orderCreateDto.getOrderPrice().getBusinessPaidPrice();//B端付费金额

	    int goodsPriceSum = 0; // 获取当前商品总金额
		int invPrice=0;//可开票金额
	    int orderCategory = orderCreateDto.getOrderCategory();//订单类型
	    if(Const.OrderCategoryType.One == orderCategory||Const.OrderCategoryType.Two == orderCategory){
	    	for(GoodsInfoDto goodsInfoDto: orderCreateDto.getGoodsDetail()) {
	    		int quantity = goodsInfoDto.getQuantity();//商品数量
	    		int price = goodsInfoDto.getPrice();//商品单价
				int canInvoiceAmount=goodsInfoDto.getCanInvoiceAmount();
	    		goodsPriceSum += quantity*price;
//				invPrice+=canInvoiceAmount;
				if(goodsInfoDto.getActualAmount()<goodsInfoDto.getCanInvoiceAmount()) {
					log.error("服务明细开票金额不能大于实付金额");
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务明细开票金额不能大于实付金额");
				}
	    	}
        }else if(Const.OrderCategoryType.Three == orderCategory){
        	for(ServiceInfoDto serviceInfoDto: orderCreateDto.getServiceDetail()) {
        		int quantity = serviceInfoDto.getQuantity();//商品数量
	    		int price = serviceInfoDto.getPrice();//商品单价
				int canInvoiceAmount=serviceInfoDto.getCanInvoiceAmount();
	    		goodsPriceSum += quantity*price;
//				invPrice+=canInvoiceAmount;
	    		if(serviceInfoDto.getActualAmount()<serviceInfoDto.getCanInvoiceAmount()) {
	    			log.error("服务明细开票金额不能大于实付金额");
	    			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务明细开票金额不能大于实付金额");
	    		}
	    	}
        }

		log.info("=========可开票金额="+invPrice);

	    int reducedPriceSum = couponReducedPrice + promotionReducedPrice + otherReducedPrice;//优惠总金额

	    int  orderPriceSum = goodsPriceSum + transportPrice - reducedPriceSum;//订单总金额

	    int finalPriceSum = orderPriceSum - integralPrice - businessPaidPrice;//最终应付金额

	    int totalOrderPrice = orderCreateDto.getOrderPrice().getTotalOrderPrice();//上送的订单总金额
	    int totalGoodsPrice = orderCreateDto.getOrderPrice().getTotalGoodsPrice();//上送的商品总金额
	    int totalReducedPrice = orderCreateDto.getOrderPrice().getTotalReducedPrice();//上送的优惠总金额
	    int finalPrice = orderCreateDto.getOrderPrice().getFinalPrice();//上送的最终应付金额

		if(reducedPriceSum != totalReducedPrice) {//校验优惠总金额
			log.error("当前优惠总金额为{};上送优惠总金额为{}",reducedPriceSum, totalReducedPrice);
			throw new OrderServiceException(OrderServiceCode.REDUCED_PRICE_DISAFFINITY);
		}
		if(goodsPriceSum != totalGoodsPrice) {//校验商品总金额
			log.error("当前商品总金额为{};上送商品总金额为{}",goodsPriceSum, totalGoodsPrice);
			throw new OrderServiceException(OrderServiceCode.GOODS_PRICE_DISAFFINITY);
		}
		if(orderPriceSum != totalOrderPrice) {//校验订单总金额
			log.error("当前订单总金额为{};上送订单总金额为{}",orderPriceSum, totalOrderPrice);
			throw new OrderServiceException(OrderServiceCode.ORDER_PRICE_DISAFFINITY);
		}
		if(finalPriceSum != finalPrice) {//校验最终应付金额
			log.error("当前最终应付金额为{};上送最终应付金额为{}",finalPriceSum, finalPrice);
			throw new OrderServiceException(OrderServiceCode.FINAL_PRICE_DISAFFINITY);
		}


	    int invoicePrice = orderCreateDto.getOrderPrice().getInvoicePrice();//开票金额
	    if(finalPrice < invoicePrice) {
	    	log.error("当前最终应付金额为{};开票金额为{}", finalPrice, invoicePrice);
	    	throw new OrderServiceException(OrderServiceCode.IVOICE_PRICE_OVER_FINAL);
	    }
//	    if(invPrice!=invoicePrice){
//			log.error("==商品总金额为{};开票金额为{}", invPrice, invoicePrice);
//			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR,"商品总金额与开票金额不符");
//		}
	}

	public static void validOrderPayDto(OrderPayDto dto) {
		if(4==dto.getPayType() && !StringUtil.isNotEmpty(dto.getOpenId())) {
			log.error("payType=4时，openId必传");
			throw new OrderServiceException(OrderServiceCode.PAYTYPE4_OPENID_NOTNULL);
		}
	}


	public static void validLengthAndRegex(OrderCreateDto orderCreateDto) {
		orderCreateNotBlank(orderCreateDto);
		if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",orderCreateDto.getRequestId())) {
			log.error("requestID格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "请求流水号格式错误");
		}
		if (!Pattern.matches("[0-9 a-z A-Z]{1,32}",orderCreateDto.getExternalOrderSerial())) {
			log.error("externalOrderSerial格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "外部业务订单号格式错误");
		}

		if (!Pattern.matches("[0-9]{1,20}",orderCreateDto.getBusinessSource())) {
			log.error("externalOrderSerial格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "业务系统标识格式错误");
		}

		if (!Pattern.matches("^0(1|2|3|4|5)$",orderCreateDto.getTriggerType())) {
			log.error("triggerType格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "用户触点类型格式错误");
		}

		if (!Pattern.matches("^0(1|2)$",orderCreateDto.getCustomerSource())) {
			log.error("客户渠道格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "客户渠道格式错误");
		}
		if (!Pattern.matches("^(1|2)$",String.valueOf(orderCreateDto.getPaymentMode()))) {
			log.error("订单支付类型格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单支付类型格式错误");
		}

		if (!Pattern.matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$",orderCreateDto.getOrderTime())) {
			log.error("orderTime格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "下单时间类型格式错误");
		}
		if (!Pattern.matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$",orderCreateDto.getExpireTime())) {
			log.error("expireTime格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "超时时间类型格式错误");
		}
		int orderCategory = orderCreateDto.getOrderCategory();//订单类型
		log.info("---------------订单类型:{}",orderCategory);
		if (!Pattern.matches("^(1|2|3)$",String.valueOf(orderCategory))) {
			log.error("订单类型格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单类型格式错误");
		}
		//虚拟商品和洗停充服务信息判断
		if((Const.OrderCategoryType.One == orderCategory|| Const.OrderCategoryType.Two == orderCategory )&&
				(orderCreateDto.getGoodsDetail() == null || orderCreateDto.getGoodsDetail().size() <= 0)){
			throw new OrderServiceException(OrderServiceCode.ORDER_CATEGORY_ERROR);
		}else if(Const.OrderCategoryType.Three == orderCategory && (orderCreateDto.getServiceDetail() == null || orderCreateDto.getServiceDetail().size() <= 0)){
			throw new OrderServiceException(OrderServiceCode.ORDER_CATEGORY_ERROR);
		}
		validGoodDetail(orderCreateDto);
		validServiceInfo(orderCreateDto);
		validOrderPrice(orderCreateDto);
	}

	private static void orderCreateNotBlank(OrderCreateDto orderCreateDto) {
		if (StringUtils.isBlank(orderCreateDto.getRequestId())) {
			log.error("请求流水号不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "请求流水号不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getExternalOrderSerial())) {
			log.error("外部业务订单号不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "外部业务订单号不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getBusinessSource())) {
			log.error("业务系统标识不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "业务系统标识不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getTriggerType())) {
			log.error("用户触点类型不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "用户触点类型不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getCustomerSource())) {
			log.error("客户渠道不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "客户渠道不能为空");
		}
		if (orderCreateDto.getOrderCategory()==null) {
			log.error("订单类型不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单类型不能为空");
		}
		if (orderCreateDto.getPaymentMode()==null) {
			log.error("订单支付类型不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单支付类型不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getOrderTime())) {
			log.error("下单时间不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "下单时间不能为空");
		}
		if (StringUtils.isBlank(orderCreateDto.getExpireTime())) {
			log.error("超时时间不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "超时时间不能为空");
		}

	}

	private static void validOrderPrice(OrderCreateDto orderCreateDto) {
		OrderPriceDto dto=orderCreateDto.getOrderPrice();
		if(null == dto.getTotalOrderPrice()){
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单总金额不能为空");
		}else{
			if( dto.getTotalOrderPrice()<0){
				throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "订单总金额不能小于0");
			}
		}
		if(null == dto.getTotalGoodsPrice()){
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品总金额不能为空");
		}else{
			if( dto.getTotalGoodsPrice()<0){
				throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品总金额不能小于0");
			}
		}
		if (null == dto.getTransportPrice() || dto.getTransportPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "运费金额不能小于0");
		}
		if (null == dto.getTotalReducedPrice() || dto.getTotalReducedPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "优惠总金额不能小于0");
		}
		if (null == dto.getCouponReducedPrice() || dto.getCouponReducedPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "优惠券抵扣金额不能小于0");
		}
		if (null == dto.getPromotionReducedPrice() || dto.getPromotionReducedPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "促销优惠金额不能小于0");
		}
		if (null == dto.getOtherReducedPrice() || dto.getOtherReducedPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "优惠金额不能小于0");
		}
		if (null == dto.getBusinessPaidPrice() || dto.getBusinessPaidPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "B端付费金额不能小于0");
		}
		if (null == dto.getIntegralPrice() || dto.getIntegralPrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "积分折现金额不能小于0");
		}
		if(null == dto.getFinalPrice()){
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "最终应付金额不能为空");
		}else{
			if( dto.getFinalPrice()<0){
				throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "最终应付金额不能小于0");
			}
		}
		if (null == dto.getInvoicePrice() || dto.getInvoicePrice() <0) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "可开票金额不能小于0");
		}
	}

	private static void validServiceInfo(OrderCreateDto orderCreateDto) {
		List<ServiceInfoDto> serviceInfoDto= orderCreateDto.getServiceDetail();
		if (serviceInfoDto!=null&&serviceInfoDto.size()>0) {
			serviceInfoDto.forEach(dto->{
				if (StringUtils.isBlank(dto.getGoodsItemSerial())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务类目序列号不能为空");
				} else {
					if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",dto.getGoodsItemSerial())) {
						log.error("服务类目序列号格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务类目序列号格式错误");
					}
				}
				if (StringUtils.isBlank(dto.getServiceSerial())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务序列号不能为空");
				} else {
					if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",dto.getServiceSerial())) {
						log.error("服务序列号格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务序列号格式错误");
					}
				}
				if (StringUtils.isBlank(dto.getServiceName())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务名称不能为空");
				} else {
					if(dto.getServiceName().getBytes().length>255){
						log.error("服务名称格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务名称格式错误");
					}
				}
				if (null == dto.getQuantity() || dto.getQuantity() <=0) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务数量需大于0");
				}
				if (StringUtils.isBlank(dto.getUnit())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务计量单位不能为空");
				} else {
					if (!Pattern.matches( "^[A-Za-z\\u4e00-\\u9fa5]{1,11}+$",dto.getUnit())) {
						log.error("unit格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "服务计量单位格式错误");
					}
				}
				if (null == dto.getPrice() || dto.getPrice() <0) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品单价不能小于0");
				}
				if (isNotEmpty(dto.getSpecification())&&dto.getSpecification().getBytes().length > 2000) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品规格格式错误");
				}
				if (isNotEmpty(dto.getGoodsPictureUrl())&&dto.getGoodsPictureUrl().getBytes().length > 2000) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品缩略图url长度为0-2000");
				}
				if (isNotEmpty(dto.getGoodsSnapshot())&&dto.getGoodsSnapshot().getBytes().length > 2000) {
					log.error("goodsSnapshot格式错误");
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品快照格式错误");
				}
				validateAmount(dto.getActualAmount(),dto.getCanInvoiceAmount());

			});
		}
	}

	private static void validGoodDetail(OrderCreateDto orderCreateDto) {
		List<GoodsInfoDto> goodsInfoDto= orderCreateDto.getGoodsDetail();
		if (goodsInfoDto!=null&&goodsInfoDto.size()>0) {
			goodsInfoDto.forEach(dto->{
				if (StringUtils.isBlank(dto.getGoodsItemSerial())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品类目序列号不能为空");
				} else {
					if (!Pattern.matches("^0(1|2)$",dto.getGoodsItemSerial())) {
						log.error("goodsItemSerial格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品类目序列号格式错误");
					}
				}
				if (StringUtils.isBlank(dto.getGoodsSerial())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品序列号不能为空");
				} else {
					if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",dto.getGoodsSerial())) {
						log.error("goodsSerial格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品序列号格式错误");
					}
				}
				if (StringUtils.isBlank(dto.getGoodsName())) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品名不能为空");
				} else {
					if(dto.getSpecification().getBytes().length>255 ){
						log.error("goodsName格式错误");
						throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品名格式错误");
					}
				}

				if (null == dto.getQuantity() || dto.getQuantity() <=0) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品数量需大于0");
				}
				if (null == dto.getPrice() || dto.getPrice() <0) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品单价不能小于0");
				}
				if (null == dto.getMarketPrice() || dto.getMarketPrice() <0) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "市场价不能小于0");
				}
				if (isNotEmpty(dto.getGoodsSnapshot())&& dto.getSpecification().getBytes().length > 2000) {
					log.error("goodsSnapshot格式错误");
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品快照格式错误");
				}
				if (isNotEmpty(dto.getSpecification())&&dto.getSpecification().getBytes().length > 2000) {
					throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品规格长度不能大于2000");
				}
				validateAmount(dto.getActualAmount(),dto.getCanInvoiceAmount());

			});
		}
	}

	private static void validateAmount(Integer actualAmount,Integer canInvoiceAmount) {
		if (null == actualAmount) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "实付金额不能为空");
		}else{
			if (actualAmount<0) {
				throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "实付金额不能小于0");
			}
		}
		if (null == canInvoiceAmount) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品可开票金额不能为空");
		}else{
			if (canInvoiceAmount<0) {
				throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "商品可开票金额不能小于0");
			}
		}
	}

	public static void validOcLengthAndRegex(OrderCloseDto orderCloseDto) {
		validReqAndUos(orderCloseDto.getRequestId(),orderCloseDto.getUniformOrderSerial());
	}

	public static void validSdiLengthAndRegex(SyncDeliveryInfoDto syncDeliveryInfoDto) {
		validReqAndUos(syncDeliveryInfoDto.getRequestId(),syncDeliveryInfoDto.getUniformOrderSerial());
		syncDeliveryInfoIsNotBlank(syncDeliveryInfoDto);
		if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",syncDeliveryInfoDto.getDeliverySerial())) {
			log.error("交付流水号格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付流水号格式错误");
		}
		if (isNotEmpty(syncDeliveryInfoDto.getDeliveryContent())&&syncDeliveryInfoDto.getDeliveryContent().getBytes().length > 500) {
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付内容格式错误");
		}
		if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",syncDeliveryInfoDto.getDeliveryChannel())) {
			log.error("交付渠道id格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付渠道id格式错误");
		}
		if (!Pattern.matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$",syncDeliveryInfoDto.getDeliveryFinishTime())) {
			log.error("交付完成时间格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付完成时间格式错误");
		}
	}

	private static void syncDeliveryInfoIsNotBlank(SyncDeliveryInfoDto syncDeliveryInfoDto) {
		if (StringUtils.isBlank(syncDeliveryInfoDto.getDeliverySerial())) {
			log.error("交付流水号不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付流水号不能为空");
		}
		if (StringUtils.isBlank(syncDeliveryInfoDto.getDeliveryContent())) {
			log.error("交付内容不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付内容不能为空");
		}
		if (StringUtils.isBlank(syncDeliveryInfoDto.getDeliveryChannel())) {
			log.error("交付渠道id不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付渠道id不能为空");
		}
		if (StringUtils.isBlank(syncDeliveryInfoDto.getDeliveryFinishTime())) {
			log.error("交付完成时间不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "交付完成时间不能为空");
		}
	}

	private static void validReqAndUos(String requestId,String uniformOrderSerial) {
		if (StringUtils.isBlank(requestId)) {
			log.error("请求流水号不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "请求流水号不能为空");
		}
		if (StringUtils.isBlank(uniformOrderSerial)) {
			log.error("统一订单号不能为空");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "统一订单号不能为空");
		}
		if (!Pattern.matches("[0-9 a-z A-Z]{1,255}",requestId)) {
			log.error("requestID格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "请求流水号格式错误");
		}
		if (!Pattern.matches("[0-9 a-z A-Z]{1,32}",uniformOrderSerial)) {
			log.error("uniformOrderSerial格式错误格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "统一订单号格式错误");
		}
	}

	public static void validOrderPrePayInfo(OrderPrePayInfoDto orderPrePayInfoDto) {
		if(orderPrePayInfoDto.getPrePayId().length()>32) {
			log.error("预支付id格式错误");
			throw new OrderServiceException(CommonServiceCode.COMMON_PARAMETER_ERROR, "预支付id格式错误");
		}
	}

	//判断字符串是否为空
	public static boolean isNotEmpty(String param) {
		if (StringUtils.isNotBlank(param)) return true;
		else return false;
	}


}
